const utils = require('./unils.js')
let fmarkers = []
let timer = 0
Page({
  data: {
    longitude: 0,
    latitude: 0,
    running: false,
    interval: 1000,
    playbackrate: 500,
    meters: 0,
    seconds: 0,
    markers: [],
    polyline: [{
      points: [],
      color: '#FF0000DD',
      width: 2
    }]
  },
  onLoad() {
    wx.getLocation({
      type: 'gcj02'
    }).then(res => {
      console.log(res)
      this.setData({
        latitude: res.latitude,
        longitude: res.longitude
      })
    })
    setInterval(this.record, this.data.interval)
  },
  run() {
    this.setData({
      running: !this.data.running
    })
  },
  record() {
    if (!this.data.running) {
      return
    }
    this.setData({
      seconds: this.data.seconds + this.data.interval / 1000
    })
    wx.getLocation({
      type: 'gcj02'
    }).then(res => {
      let newMarker = {
        longitude: res.longitude,
        latitude: res.latitude,
        iconPath: 'redPoint.png',
        width: 10,
        height: 10
      }
      let pace = 0
      let Imarkers = this.data.markers
      if (Imarkers.length > 0) {
        let lastMarker = Imarkers.slice(-1)[0]
        pace = utils.getDistance(lastMarker.latitude, lastMarker.longitude, newMarker.latitude, newMarker.longitude)
        console.log(pace)
        if (pace < 15) {
          pace = 0
        } else {
          Imarkers.push(newMarker)
        }
      } else {
        Imarkers.push(newMarker)
      }
      this.setData({
        longitude: res.longitude,
        latitude: res.latitude,
        markers: Imarkers,
        meters: this.data.meters + pace
      })
    })
  },
  save() {
    this.setData({
      running: false
    })
    wx.setStorage({
      data: this.data.markers,
      key: 'running',
    }).then(() => {
      wx.showToast({
        title: '保存成功'
      })
    })
    //新添加的代码
    let a = []
    a.push(this.data.seconds)
    a.push(this.data.meters)
    wx.setStorage({
      data: a,
      key: 'a',
    })//
  },
  playback() {
    this.setData({
      running: false
    })
    //新添加的代码
    wx.getStorage({
      key: 'a',
    }).then(res=>{
      console.log(res)
      this.setData({
        seconds:res.data[0],
        meters:res.data[1]
      })
    })//
    wx.getStorage({
      key: 'running'
    }).then(res => {
      console.log(res)
      fmarkers = res.data
      timer = setInterval(this.feedback, this.data.playbackrate)
 /*       console.log("~") */
  /*    this.clear() */
    })
    this.clear() 
  },
  feedback() {
    let Imarkers = this.data.markers
    let Ipolyline = this.data.polyline
    if (fmarkers.length > 0) {
      Imarkers.push(fmarkers.shift())
      Ipolyline[0].points = Imarkers
    } else {
      clearInterval(timer)
    }
    this.setData({
      markers: Imarkers,
      polyline: Ipolyline,
    })
  },
  clear() {
    this.setData({
      longitude: 0,
      latitude: 0,
      running: false,
      interval: 1000,
      playbackrate: 500,
      meters: 0,
      seconds: 0,
      markers: [],
      polyline: [{
        points: [],
        color: '#FF0000DD',
        width: 2
      }]
    })
  }
})